package com.own.stu.apm.agent.core.plugin.interceptor.enhence;

import com.own.stu.apm.agent.core.plugin.loader.InterceptorInstanceLoader;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.This;

@Slf4j
public class ConstructIntercept {

    ConstructAfterInterceptor constructAfterInterceptor = null;

    public ConstructIntercept(String methodInterceptor, ClassLoader classLoader) {
        try {
            InterceptorInstanceLoader.load(methodInterceptor, classLoader);
        } catch (Exception e) {
            log.error("load ConstructIntercept :{} fail ", methodInterceptor, e);
        }
    }

    @RuntimeType
    public void intercept(
            @This Object ojb,
            @AllArguments Object[] allMethodArgs
    ) throws Throwable {
        EnhenceInstanceField instance = (EnhenceInstanceField) ojb;
        try {
            constructAfterInterceptor.onConstruct(instance, allMethodArgs);
        } catch (Throwable e) {
            log.error("ConstructIntercept fail ", e);
        }
    }
}
